{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5ea4d6fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "031153df",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[68, 15,  2],\n",
       "        [83, 17, 86]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "emb = nn.Embedding(100,12)\n",
    "\n",
    "a=torch.randint(0,100,(2,3))\n",
    "a\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "42ce9ec6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 3.4499e-02,  1.8707e-02, -1.3558e-01,  4.8963e-01, -1.7991e+00,\n",
       "           4.1464e-01,  1.4799e+00,  2.0409e+00,  2.6902e-01, -6.6056e-01,\n",
       "           9.5598e-01,  1.2185e+00],\n",
       "         [ 1.9546e+00,  2.8770e-01, -6.7918e-01,  4.3467e-01, -2.0787e-01,\n",
       "           1.5512e+00, -4.6127e-01, -1.1263e+00, -1.9562e-01,  7.5936e-01,\n",
       "          -7.2178e-01,  8.2315e-02],\n",
       "         [-1.3260e+00, -3.7274e-01, -3.8782e-01,  1.6590e+00,  8.6026e-01,\n",
       "           7.0814e-01,  2.9247e+00,  6.2448e-01,  6.7435e-01, -6.7586e-01,\n",
       "           1.7997e-02,  8.1612e-02]],\n",
       "\n",
       "        [[-1.3211e+00, -1.6527e-01,  3.3668e-01,  1.0185e+00,  7.0798e-01,\n",
       "           1.1828e+00,  9.1273e-01,  1.3201e+00, -1.3149e+00,  8.6878e-01,\n",
       "          -8.5114e-01,  1.4835e+00],\n",
       "         [ 2.4956e-02, -1.2233e+00, -1.2455e+00, -2.9027e-01,  1.1752e-01,\n",
       "          -5.0676e-02, -7.6619e-02,  1.0414e+00, -1.1413e+00,  1.5021e+00,\n",
       "          -4.2643e-01, -5.2247e-01],\n",
       "         [-1.4860e-01,  6.9194e-01, -4.3782e-01,  5.3224e-01, -1.1987e-01,\n",
       "           2.0253e+00,  1.2855e-01, -1.9055e-01, -2.3597e-03, -1.3260e+00,\n",
       "          -2.1666e-02, -1.4543e-01]]], grad_fn=<EmbeddingBackward0>)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "emb(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a9119656",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.2211, -0.0221, -0.4009,  0.8611, -0.3822,  0.8913,  1.3144,  0.5130,\n",
       "          0.2493, -0.1923,  0.0841,  0.4608],\n",
       "        [-0.4816, -0.2322, -0.4489,  0.4202,  0.2352,  1.0525,  0.3216,  0.7236,\n",
       "         -0.8195,  0.3483, -0.4331,  0.2719]], grad_fn=<MeanBackward1>)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "emb(a).mean(dim=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a655f9bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "(a.unsqueeze(0) + b.unsqueeze(1))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torch_py38",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.7rc1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
